package br.com.sovi.gwt.server.jdo.controleunitario.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.sovi.gwt.client.entity.ControleUnitario;
import br.com.sovi.gwt.client.entity.ControleUnitarioPoint;

@SuppressWarnings("serial")
public class RecordServlet extends HttpServlet {

	private SimpleDateFormat sdf = new SimpleDateFormat("hhmmssddMMyyyy");

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter w = resp.getWriter();

		String descricao = req.getParameter("descricao");
		String data = req.getParameter("data");

		Date dataDate = null;
		try {
			dataDate = sdf.parse(data);
		}
		catch (ParseException e) {
			System.out.println("Erro ao decompor data.");
		}

		String[] parameterValues = req.getParameterValues("pontos");

		w.write("Pontos: " + parameterValues.length + "<br>");

		ControleUnitarioPoint[] points = new ControleUnitarioPoint[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			String string = parameterValues[i];
			String[] ll = string.split(",");

			w.write("Ponto [" + i + "]: " + string + "<br>");

			Date dataPonto = null;
			try {
				dataPonto = sdf.parse(ll[0]);
			}
			catch (ParseException e) {
				e.printStackTrace();
			}

			points[i] = new ControleUnitarioPoint(dataPonto, Double.parseDouble(ll[1]), Double.parseDouble(ll[2]));
		}

		List<ControleUnitario> controlesUnitarios = ControleUnitarioServiceImpl.controlesUnitarios;
		
		if (controlesUnitarios == null) {
			controlesUnitarios = new ArrayList<ControleUnitario>();
		}

		ControleUnitario controleUnitario = findControleUnitario(controlesUnitarios, descricao);
		
		if (controleUnitario == null) {
			System.out.println("Não encontrado");
			controleUnitario = new ControleUnitario(descricao, dataDate, false, true, 11, points);
			controlesUnitarios.add(controleUnitario);
		} else {
			System.out.println("Encontrado");
			controleUnitario.getPoints().addAll(Arrays.asList(points));
		}

		
		w.write("Numero de Pontos: " + controleUnitario.getPoints().size() + "<br>" + ControleUnitarioServiceImpl.controlesUnitarios.size());
		w.flush();
		w.close();
	}

	private ControleUnitario findControleUnitario(List<ControleUnitario> controlesUnitarios, String descricao) {
		if (controlesUnitarios == null || controlesUnitarios.isEmpty()) {
			return null;
		}
		
		for (ControleUnitario controleUnitario : controlesUnitarios) {
			if (controleUnitario.getDescricao().equals(descricao)) {
				return controleUnitario;
			}
		}
		
		return null;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.execute(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.execute(req, resp);
	}

}
